﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

public partial class Admin_Quan_Ly_DDHANG : System.Web.UI.Page
{
    DataProvider duLieu = new DataProvider();
    ListBox listBox = new ListBox();

    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (Request.QueryString["id"] != null && Request.QueryString["man"] == "xem")
        {
            int id = int.Parse(Request.QueryString["id"]);

            listBox.Items.Clear();
            duLieu.MakeDuLieu(listBox, id.ToString());

            DataTable bangchitietddh = duLieu.Load_Du_Lieu_Admin_New("SELECT CHITIETDDHANG.MADDH, CHITIETDDHANG.MAHH, HANGHOA.TENHH, CHITIETDDHANG.DONGIABAN, CHITIETDDHANG.SLDAT FROM CHITIETDDHANG INNER JOIN HANGHOA ON CHITIETDDHANG.MAHH = HANGHOA.MAHH where maddh=?", listBox);

            thongbaotrantrung.Text = "";
            if (bangchitietddh.Rows.Count != 0)
            {
                xemchitiet.DataSource = bangchitietddh;
                xemchitiet.DataBind();
                panelxemchitiet.Visible = true;
            }
            else
            {
                panelxemchitiet.Visible = false;
                thongbaotrantrung.Text = "<font color='red'>Đơn Hàng Này Không Có Món Hàng Nào.</font><hr/>";
            }
        }

        if (!IsPostBack)
        {
            Load_Danh_Muc();
        }
    }
    public void Load_Danh_Muc()
    {
        DataTable bangchude = null;
        bangchude = duLieu.Load_Du_Lieu_Admin_New("select * from DONDATHANG order by maddh DESC", listBox);
        if (bangchude.Rows.Count != 0)
        {
            gvDuLieu.DataSource = bangchude;
            gvDuLieu.DataBind();
        }
        else
        {
            thongbaotrantrung.Text = "<font color='red'>Hiện Tại Trang Web Chưa Có Đơn Hàng Nào.</font>";
        }
    }

    protected void gvDuLieu_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        gvDuLieu.PageIndex = e.NewPageIndex;
        Load_Danh_Muc();
    }

    protected void gvDuLieu_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
    {
        gvDuLieu.EditIndex = -1;
        Load_Danh_Muc();
    }


    protected void gvDuLieu_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
    {
        listBox.Items.Clear();
        duLieu.MakeDuLieu(listBox, gvDuLieu.Rows[e.RowIndex].Cells[0].Text);
        if (duLieu.Luu_Du_Lieu_Admin_New("delete from CHITIETDDHANG where maddh=?", listBox) != 0)
        {
            listBox.Items.Clear();
            duLieu.MakeDuLieu(listBox, gvDuLieu.Rows[e.RowIndex].Cells[0].Text);

            if (duLieu.Luu_Du_Lieu_Admin_New("delete from DONDATHANG where maddh=?", listBox) == 0)
            {
                thongbaotrantrung.Text = "<font color='red'>Xóa Không Thành Công.</font><hr/>";
                return;
            }
        }
        else
        {
            thongbaotrantrung.Text = "<font color='red'>Xóa Không Thành Công.</font><hr/>";
            return;
        }
        Response.Redirect("Quan_Ly_DDHANG.aspx");
    }

    protected void gvDuLieu_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        gvDuLieu.EditIndex = e.NewEditIndex;
        Load_Danh_Muc();
    }

    protected void gvDuLieu_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
    {
        bool dagiao = ((CheckBox)gvDuLieu.Rows[e.RowIndex].Cells[10].FindControl("dagiao")).Checked;
        int dagiao1 = 0;

        if (dagiao == true)
        {
            dagiao1 = 1;
        }
        else
        {
            dagiao1 = 0;
        }

        listBox.Items.Clear();
        duLieu.MakeDuLieu(listBox, dagiao1.ToString());
        duLieu.MakeDuLieu(listBox, gvDuLieu.Rows[e.RowIndex].Cells[0].Text);

        if (duLieu.Luu_Du_Lieu_Admin_New("update DONDATHANG set dagiao=? where maddh=?", listBox) == 0)
        {
            thongbaotrantrung.Text = "<font color='red'>Lưu Không Thành Công.</font><hr/>";
        }

        gvDuLieu.EditIndex = -1;
        Load_Danh_Muc();

    }

    protected void gvDuLieu_Sorting(object sender, GridViewSortEventArgs e)
    {
        string LenhSql = "select * from DONDATHANG";

        if (!string.IsNullOrEmpty(e.SortExpression))
        {
            LenhSql = LenhSql + " Order by " + e.SortExpression;
        }

        gvDuLieu.DataSource = duLieu.Load_Du_Lieu_Admin_New(LenhSql, listBox);
        gvDuLieu.DataBind();
    }
}